package client.gui.panels;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.util.ArrayList;

import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFileChooser;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.ScrollPaneConstants;
import javax.swing.text.DefaultStyledDocument;

import com.jgoodies.forms.factories.DefaultComponentFactory;

import controllers.ControllerIF;
import controllers.DocumentSizeFilter;
import entities.InfoSystem;

public class NewRequest extends JPanel implements ActionListener, PanelIF {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private Font font1 = new Font("Tahoma", Font.PLAIN, 14);
	private Font font2 = new Font("Tahoma", Font.PLAIN, 12);
	private int maxCharacters = 250;

	private ControllerIF panelController;
	private ArrayList<InfoSystem> infoSystems;

	private JButton btnClear;
	private JButton btnSubmit;
	private JButton btnOpen;
	private JButton btnOpen2;
	private JTextArea existingTextArea;
	private JTextArea requestedTextArea;
	private JTextArea reasonTextArea;
	private JTextArea commentsTextArea;
	private JComboBox<String> systemComboBox;
	private JTextField filePath;
	private JTextField filePath2;
	private JFileChooser fc;
	private JFileChooser fc2;
	private ArrayList<File> fileArr;
	private JLabel SystemLabel;
	private JLabel lblExistingLabel;
	private JLabel lblRequestedLabel;
	private JLabel lblReasonFor;
	private DefaultStyledDocument docExisting;
	private DefaultStyledDocument docRequested;
	private DefaultStyledDocument docReason;
	private DefaultStyledDocument docComments;

	public NewRequest(ArrayList<InfoSystem> infoSystems) {
		this.infoSystems = infoSystems;
		initGUI();
	}
	
	public void initGUI() {

		setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
		
		/*********************************
		 ** Max characters input filter **
		 ********************************/
		docExisting = new DefaultStyledDocument();
        docExisting.setDocumentFilter(new DocumentSizeFilter(maxCharacters));
        docRequested = new DefaultStyledDocument();
        docRequested.setDocumentFilter(new DocumentSizeFilter(maxCharacters));
        docReason = new DefaultStyledDocument();
        docReason.setDocumentFilter(new DocumentSizeFilter(maxCharacters));
        docComments = new DefaultStyledDocument();
        docComments.setDocumentFilter(new DocumentSizeFilter(maxCharacters));
		
		/*****************
		 ** Title panel **
		 ****************/
        
        JPanel titlePanel = new JPanel();
		
		JLabel lblTitleLabel = DefaultComponentFactory.getInstance().createTitle("New Request");		
		lblTitleLabel.setFont(new Font("Tahoma", Font.BOLD, 18));
		titlePanel.add(lblTitleLabel);

		add(Box.createVerticalStrut(20));

		/****************************
		 ** System combo box panel **
		 ***************************/
		JPanel System_panel = new JPanel();
		add(System_panel);
		System_panel.setLayout(new BoxLayout(System_panel, BoxLayout.X_AXIS));

			SystemLabel = new JLabel("*System");
			SystemLabel.setFont(font1);
			SystemLabel.setOpaque(true);
			System_panel.add(SystemLabel);
	
			System_panel.add(Box.createHorizontalStrut(159));
	
			systemComboBox = new JComboBox<String>();
			systemComboBox.setPreferredSize(new Dimension(100, 20));
			systemComboBox.addItem("");
			for(int i = 0; i < infoSystems.size(); i++) {
				systemComboBox.addItem(infoSystems.get(i).getS_name());
			}
	
			/*
			 * if (comboBox.getSelectedItem() == "System 1"){
			 * System.out.println("System 1 selected"); }
			 */
	
			System_panel.add(systemComboBox);
	
			System_panel.add(Box.createHorizontalGlue());

		add(Box.createVerticalStrut(30));

		/******************************************************
		 ** "Existing situation description" text area panel **
		 *****************************************************/
		JPanel existingPanel = new JPanel();
		add(existingPanel);
		existingPanel.setLayout(new BoxLayout(existingPanel, BoxLayout.X_AXIS));

			lblExistingLabel = new JLabel("*Existing situation description:");
			lblExistingLabel.setFont(font1);
			lblExistingLabel.setOpaque(true);
			existingPanel.add(lblExistingLabel);

			existingPanel.add(Box.createHorizontalStrut(33));
	
			JScrollPane existingScrollPane = new JScrollPane();
			existingPanel.add(existingScrollPane);
	
			existingTextArea = new JTextArea();
			existingTextArea.setLineWrap(true);
			existingTextArea.setRows(6);
			existingTextArea.setFont(font2);
			existingTextArea.setDocument(docExisting);
			existingScrollPane.setViewportView(existingTextArea);

			add(Box.createVerticalStrut(30));

		/****************************************************
		 ** "Requested change description" text area panel **
		 ***************************************************/
		JPanel requestedPanel = new JPanel();
		add(requestedPanel);
		requestedPanel.setLayout(new BoxLayout(requestedPanel, BoxLayout.X_AXIS));

			lblRequestedLabel = new JLabel("*Requested change description:");
			lblRequestedLabel.setFont(font1);
			lblRequestedLabel.setOpaque(true);
			requestedPanel.add(lblRequestedLabel);
	
			requestedPanel.add(Box.createHorizontalStrut(20));
	
			JScrollPane requestedScrollPane = new JScrollPane();
			requestedPanel.add(requestedScrollPane);
	
			requestedTextArea = new JTextArea();
			requestedTextArea.setLineWrap(true);
			requestedTextArea.setRows(6);
			requestedTextArea.setFont(font2);
			requestedTextArea.setDocument(docRequested);
			requestedScrollPane.setViewportView(requestedTextArea);

		add(Box.createVerticalStrut(30));

		/******************************************
		 ** "Reason for request" text area panel **
		 *****************************************/
		JPanel reasonPanel = new JPanel();
		add(reasonPanel);
		reasonPanel.setLayout(new BoxLayout(reasonPanel, BoxLayout.X_AXIS));

			lblReasonFor = new JLabel("*Reason for request:");
			lblReasonFor.setFont(font1);
			lblReasonFor.setOpaque(true);
			reasonPanel.add(lblReasonFor);
	
			reasonPanel.add(Box.createHorizontalStrut(87));
			
			JScrollPane reasonScrollPane = new JScrollPane();
			reasonPanel.add(reasonScrollPane);
			
			reasonTextArea = new JTextArea();
			reasonTextArea.setLineWrap(true);
			reasonTextArea.setRows(6);
			reasonTextArea.setFont(font2);
			reasonTextArea.setDocument(docReason);
			reasonScrollPane.setViewportView(reasonTextArea);

		add(Box.createVerticalStrut(20));

		/******************************
		 ** Comments text area panel **
		 *****************************/
		JPanel commentsPanel = new JPanel();
		add(commentsPanel);
		commentsPanel.setLayout(new BoxLayout(commentsPanel, BoxLayout.X_AXIS));

			JLabel lblComments = new JLabel("Comments:");
			lblComments.setFont(font1);
			commentsPanel.add(lblComments);
	
			commentsPanel.add(Box.createHorizontalStrut(147));
			
			JScrollPane commentsScrollPane = new JScrollPane();
			commentsPanel.add(commentsScrollPane);
			
			commentsTextArea = new JTextArea();
			commentsTextArea.setRows(6);
			commentsTextArea.setFont(font2);
			commentsTextArea.setLineWrap(true);
			commentsTextArea.setDocument(docComments);
			commentsScrollPane.setViewportView(commentsTextArea);
		
		add(Box.createVerticalStrut(20));
		
		/**********************************
		 ** Uploading files panels panel **
		 *********************************/
		JPanel browseOuterPanel = new JPanel();
		browseOuterPanel.setLayout(new BorderLayout(0, 0));
		browseOuterPanel.setPreferredSize(new Dimension(450, 250));
		add(browseOuterPanel);
		
			JScrollPane browseScrollPane = new JScrollPane();
			browseOuterPanel.add(browseScrollPane, BorderLayout.CENTER);
			browseScrollPane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
			
			/***************************************************
			 ** Uploading files panels nested in Y_AXIS panel **
			 **************************************************/
			JPanel browseOuterPanel2 = new JPanel();
			browseScrollPane.setViewportView(browseOuterPanel2);
			browseOuterPanel2.setLayout(new BoxLayout(browseOuterPanel2, BoxLayout.Y_AXIS));
			
				/***************************
				 ** First open file panel **
				 **************************/
				JPanel browseInnerPanel = new JPanel();
				browseInnerPanel.setLayout(new BoxLayout(browseInnerPanel, BoxLayout.X_AXIS));
				browseOuterPanel2.add(browseInnerPanel);
					
					JLabel lblOpenFile = new JLabel("Upload File");
					lblOpenFile.setAlignmentX(0);
					lblOpenFile.setAlignmentY((float) 0.5);
					lblOpenFile.setFont(font1);
					browseInnerPanel.add(lblOpenFile);
					
					browseInnerPanel.add(Box.createHorizontalStrut(155));
					
					filePath = new JTextField("Choose File...");
					browseInnerPanel.add(filePath);
					filePath.setColumns(50);
					filePath.setMaximumSize(new Dimension(1280, 25));
					
					browseInnerPanel.add(Box.createHorizontalStrut(20));
					
					btnOpen = new JButton("Browse");
					browseInnerPanel.add(btnOpen);
					btnOpen.addActionListener(this);
					
					fc = new JFileChooser();
			
				/****************************
				 ** Second open file panel **
				 ***************************/
				JPanel browseInnerPanel2 = new JPanel();
				browseInnerPanel2.setLayout(new BoxLayout(browseInnerPanel2, BoxLayout.X_AXIS));
				browseOuterPanel2.add(browseInnerPanel2);
					
					JLabel lblOpenFile2 = new JLabel("Upload File");
					lblOpenFile2.setAlignmentX(0);
					lblOpenFile2.setAlignmentY((float) 0.5);
					lblOpenFile2.setFont(font1);
					browseInnerPanel2.add(lblOpenFile2);
					
					browseInnerPanel2.add(Box.createHorizontalStrut(155));
					
					filePath2 = new JTextField("Choose File...");
					browseInnerPanel2.add(filePath2);
					filePath2.setColumns(50);
					filePath2.setMaximumSize(new Dimension(1280, 25));
					
					browseInnerPanel2.add(Box.createHorizontalStrut(20));
					
					btnOpen2 = new JButton("Browse");
					browseInnerPanel2.add(btnOpen2);
					btnOpen2.addActionListener(this);
			
					fc2 = new JFileChooser();

		add(Box.createVerticalStrut(10));

		/***************************
		 ** Buttons holding panel **
		 **************************/
		JPanel buttonsPanel = new JPanel();
		add(buttonsPanel);
		buttonsPanel.setLayout(new BoxLayout(buttonsPanel, BoxLayout.X_AXIS));
		
			JLabel lblRequiredFields = new JLabel("* Required fields");
			lblRequiredFields.setFont(new Font("Tahoma", Font.PLAIN, 10));
			buttonsPanel.add(lblRequiredFields);

			btnClear = new JButton("Clear");
			btnClear.addActionListener(this);
			
					buttonsPanel.add(Box.createHorizontalGlue());
			buttonsPanel.add(btnClear);
	
			buttonsPanel.add(Box.createHorizontalStrut(30));
	
			btnSubmit = new JButton("Submit");
			btnSubmit.addActionListener(this);
			buttonsPanel.add(btnSubmit);
	
		add(Box.createVerticalStrut(5));
		

		/*
		 * Add_Doc.addActionListener(new java.awt.event.ActionListener() {
		 * public void actionPerformed(java.awt.event.ActionEvent e) {
		 * JFileChooser chooser = new JFileChooser(); } //
		 * FileNameExtensionFilter filter = new
		 * FileNameExtensionFilter("JPG & GIF Images", "jpg", "gif"); //
		 * chooser.setFileFilter(filter); int returnVal =
		 * chooser.showOpenDialog(getParent()); if(returnVal ==
		 * JFileChooser.APPROVE_OPTION) {
		 * //System.out.println("You choose to open this file: " +
		 * chooser.getSelectedFile().getName());
		 * 
		 * filePathLabel_1.setText(chooser.getSelectedFile().getName());
		 * filePathLabel_1.setVisible(true);
		 * 
		 * 
		 * //textField.setColumns(10); // textField = new JTextField("l111l " +
		 * chooser.getSelectedFile().getName() );
		 * 
		 * }
		 * 
		 * })
		 */
	}

	
	@Override
	public void actionPerformed(ActionEvent e) {
		if (e.getSource() == btnClear) {
			systemComboBox.setSelectedItem("");
			existingTextArea.setText("");
			requestedTextArea.setText("");
			reasonTextArea.setText("");
			commentsTextArea.setText("");
			filePath.setText("Choose File...");
			filePath2.setText("Choose File...");
			SystemLabel.setForeground(Color.BLACK);			
			lblExistingLabel.setForeground(Color.BLACK);			
			lblRequestedLabel.setForeground(Color.BLACK);			
			lblReasonFor.setForeground(Color.BLACK);
			
		} 
		else if (e.getSource() == btnSubmit) {
			String[] existing = existingTextArea.getText().split(" ");
			String[] requested = requestedTextArea.getText().split(" ");
			String[] reason = reasonTextArea.getText().split(" ");
			
			/*****************************************************
			 ** Checking if all the required fields were filled **
			 ****************************************************/
			if(systemComboBox.getSelectedItem() == "" || existingTextArea.getText().isEmpty() || existing.length < 1 || 
					requestedTextArea.getText().isEmpty() || requested.length < 1 ||
					reasonTextArea.getText().isEmpty() || reason.length < 1) {
				
				if(systemComboBox.getSelectedItem() == "") {					
					SystemLabel.setForeground(Color.RED);
				}
				else {					
					SystemLabel.setForeground(Color.BLACK);
				}
				if(existingTextArea.getText().isEmpty() || existing.length < 1) {					
					lblExistingLabel.setForeground(Color.RED);
				}
				else {					
					lblExistingLabel.setForeground(Color.BLACK);
				}
				if(requestedTextArea.getText().isEmpty() || requested.length < 1) {					
					lblRequestedLabel.setForeground(Color.RED);
				}
				else {					
					lblRequestedLabel.setForeground(Color.BLACK);
				}
				if(reasonTextArea.getText().isEmpty() || reason.length < 1) {					
					lblReasonFor.setForeground(Color.RED);
				}
				else {					
					lblReasonFor.setForeground(Color.BLACK);
				}
			}
			else {
				fileArr = new ArrayList<File>();
				if(filePath.getText() != "" || filePath2.getText() != "") {
					if(filePath.getText() != "") {
						fileArr.add(fc.getSelectedFile());
					}
					if(filePath2.getText() != "") {
						fileArr.add(fc2.getSelectedFile());
					}
				}
				panelController.action();
			}
		} 
		else if (e.getSource() == btnOpen) {
			int returnVal = fc.showOpenDialog(fc.getParent());
			if(returnVal == JFileChooser.APPROVE_OPTION) {
				filePath.setText(fc.getSelectedFile().getPath());
			}
		} 
		else if (e.getSource() == btnOpen2) {
			int returnVal = fc2.showOpenDialog(fc2.getParent());
			if(returnVal == JFileChooser.APPROVE_OPTION) {
				filePath2.setText(fc2.getSelectedFile().getPath());
			}
		}

	}

	public Object getSystemComboBox() {
		return systemComboBox.getSelectedItem();
	}

	public String getExistingTextArea() {
		return existingTextArea.getText();
	}

	public String getRequestedTextArea() {
		return requestedTextArea.getText();
	}

	public String getReasonTextArea() {
		return reasonTextArea.getText();
	}

	public String getCommentsTextArea() {
		return commentsTextArea.getText();
	}

	public ArrayList<File> getFileArr() {
		return fileArr;
	}

	public ArrayList<InfoSystem> getInfoSystems() {
		return infoSystems;
	}

	public void setInfoSystems(ArrayList<InfoSystem> infoSystems) {
		this.infoSystems = infoSystems;
	}

	/**
	 * set local parameter to hold the controller reference
	 * 
	 * @param cif
	 *            Controller interface
	 */
	public void setController(ControllerIF cif) {
		panelController = cif;
	}

}
